package localFiles;

/***
 * @author Ragnvald Fuglestein
 */

import java.io.File;
import java.util.regex.Matcher;

public class PathExtractionSubPattern {
	public static enum Types {
		artist,
		album,
		none
	};
	protected String[] data;
	protected String[] regex;
	public Types type = Types.none;
	private Types[] Atype;
	public java.util.regex.Pattern[] regexPattern;
	
	
	private boolean[] AcontainsArtistName;
	private boolean[] AcontainsAlbumName;
	private boolean[] AcontainsTrackName;
	private boolean[] AcontainsAlbumYear;
	private boolean[] AcontainsTrackNum;
	private boolean[] AcontainsExtension;
	
	public boolean containsArtistName = false;
    public boolean containsAlbumName = false;
    public boolean containsTrackName = false;
    public boolean containsAlbumYear = false;
    public boolean containsTrackNum = false;
    public boolean containsExtension = false;
    
	private Matcher matcher;
	
	public PathExtractionSubPattern(String data) {
		this.data = data.split(":");
		regex = new String[this.data.length];
		Atype = new Types[this.data.length];
		regexPattern = new java.util.regex.Pattern[this.data.length];
		AcontainsArtistName = new boolean[this.data.length];
		AcontainsAlbumName = new boolean[this.data.length];
		AcontainsTrackName = new boolean[this.data.length];
		AcontainsAlbumYear = new boolean[this.data.length];
		AcontainsTrackNum = new boolean[this.data.length];
		AcontainsExtension = new boolean[this.data.length];
		
		for (int i = 0; i < this.data.length; i++) {
			add(i);
		}
	}
	private void add(int i) {
		Atype[i] = Types.none;
		regex[i] = data[i].
				replace(File.separator, java.util.regex.Pattern.quote(File.separator)).
				replace(".", "\\.").
				replace("^", "\\^").
				replace("$", "\\$").
				replace("|", "\\|").
				replace("?", "\\?").
				replace("*", "\\*").
				replace("+", "\\+").
				replace("[", "\\[").
				replace("]", "\\]").
				replace("{", "\\{").
				replace("}", "\\}").
				replace(")", "\\)").
				replace("(", "\\(");
		if (regex[i].contains("%artistname%")) {
			regex[i] = regex[i].replace("%artistname%", "(?<artistname>.+)");
			Atype[i] = Types.artist;
			AcontainsArtistName[i] = true;
		}
		if (regex[i].contains("%albumname%")) {
			regex[i] = regex[i].replace("%albumname%", "(?<albumname>.+)");
			Atype[i] = Types.album;
			AcontainsAlbumName[i] = true;
		}
		if (regex[i].contains("%trackname%")) {
			regex[i] = regex[i].replace("%trackname%", "(?<trackname>.+)");
			Atype[i] = Types.none;
			AcontainsTrackName[i] = true;
		}
		if (regex[i].contains("%albumyear%")) {
			regex[i] = regex[i].replace("%albumyear%", "(?<albumyear>\\d+)");
			AcontainsAlbumYear[i] = true;
		}
		if (regex[i].contains("%tracknum%")) {
			regex[i] = regex[i].replace("%tracknum%", "(?<tracknum>\\d+)");
			Atype[i] = Types.none;
			AcontainsTrackNum[i] = true;
		}
		if (regex[i].contains("%extension%")) {
			regex[i] = regex[i].replace("%extension%", "(?<extension>\\w+)");
			Atype[i] = Types.none;
			AcontainsExtension[i] = true;
		}
		if (regex[i].contains("%any%")) {
			regex[i] = regex[i].replace("%any%", "(.+)");
		}
		regexPattern[i] = java.util.regex.Pattern.compile(regex[i]);
	}
	
	public boolean match(String name) {
		for (int i = 0; i < this.regex.length; i++) {
			if (name.matches(regex[i])) {
				matcher = regexPattern[i].matcher(name);
				matcher.find();
				containsArtistName = AcontainsArtistName[i];
			    containsAlbumName = AcontainsAlbumName[i];
			    containsTrackName = AcontainsTrackName[i];
			    containsAlbumYear = AcontainsAlbumYear[i];
			    containsTrackNum = AcontainsTrackNum[i];
			    containsExtension = AcontainsExtension[i];
			    type = Atype[i];
				return true;
			}
		}
		return false;
	}
	
	//TODO: Check group exception
	public String getArtistName() {
		if (containsArtistName) return matcher.group("artistname").trim();
		return null;
	}
	
	public String getAlbumName() {
		if (containsAlbumName) return matcher.group("albumname").trim();
		return null;
	}
	
	public String getTrackName() {
		if (containsTrackName) return matcher.group("trackname").trim();
		return null;
	}
		
	public int getAlbumYear() {
		if (containsAlbumYear) return Integer.parseInt(matcher.group("albumyear").trim());
		return -1;
	}
	
	public int getTrackNum() {
		if (containsTrackNum) return Integer.parseInt(matcher.group("tracknum").trim());
		return -1;
	}
	
	public String getExtension() {
		if (containsExtension) return matcher.group("extension").toLowerCase().trim();
		return null;
	}
}
